package com.music.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.util.StringUtil;
import com.music.dto.Result;
import com.music.entity.Seckill;
import com.music.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Controller
@RequestMapping("/seckill")
public class SeckillController {
    @Autowired
    private AlbumService albumService;

    private  Lock lock = new ReentrantLock();
    @GetMapping("/list")
    @ResponseBody
    public Result getSeckill(){
        Result result = new Result();
        List<Seckill> seckillList = albumService.getAlbumSeckillAll(null);
        Optional.ofNullable(seckillList).ifPresent(list ->{
            list.forEach(seckill -> {

            });
        });
        if(seckillList.isEmpty()){
            result.fail(0,"没有商品");
        }else{
            result.ok(seckillList).setMessage("成功");
            //给用户加入订单

        }
        return result;
    }

    @PostMapping("/list/{quantityOfCommodity}")
    @ResponseBody
    public Result PostSeckilla(@PathVariable String quantityOfCommodity){
        Result<Object> result = new Result<>();
        if(quantityOfCommodity!= null && quantityOfCommodity.trim() != ""){
            lock.lock();
            List<Seckill> seckillList = albumService.getAlbumSeckillAll(quantityOfCommodity);
            lock.unlock();
            if(seckillList == null){
                result.fail(0,"你没抢到");
            }else{
                result.ok(200).setMessage("抢到了");
            }
        }
        return result;
    }


}
